package org.xuyuji.demo.xstream;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.time.DateFormatUtils;
import org.xuyuji.demo.xstream.bean.Person;
import org.xuyuji.demo.xstream.bean.Person.Address;
import org.xuyuji.demo.xstream.bean.Person.Friend;
import org.xuyuji.demo.xstream.bean.composite.Body;
import org.xuyuji.demo.xstream.bean.composite.CardIssuer;
import org.xuyuji.demo.xstream.bean.composite.Head;
import org.xuyuji.demo.xstream.bean.composite.Msg;
import org.xuyuji.demo.xstream.bean.extend.Body0020;
import org.xuyuji.demo.xstream.bean.extend.Body0024;
import org.xuyuji.demo.xstream.tools.XmlUtil;

public class Main {

	public static void main(String[] args) throws Exception {
		Main main = new Main();
//		main.testPerson();
//		main.testBody();
		main.testComposite();
	}
	
	/**
	 * 组装模式
	 */
	public void testComposite(){
		Msg msg = new Msg();
		
		Head head = new Head();
		head.setAppName("阿姆斯特朗回旋加速炮");
		head.setVersion("V0.01");
		head.setClientDt(DateFormatUtils.format(new Date(), "yyyyMMddHHmmss"));
		head.setSrcId("00000000");
		head.setDstId("00000001");
		head.setMsgType("0000");
		head.setMsgID(UUID.randomUUID().toString());
		
		Body body = new Body();
		body.setActName("actNaem");
		
		List<CardIssuer> cardIssuers = new ArrayList<CardIssuer>();
		cardIssuers.add(new CardIssuer("1001001"));
		cardIssuers.add(new CardIssuer("1001002"));
		cardIssuers.add(new CardIssuer("1001003"));
		body.setCardIssuers(cardIssuers);
		
		msg.setHead(head);
		msg.setBody(body);
		
		System.out.println(XmlUtil.toXml(msg));
	}
	
	public void testBody(){
        Body0024 body0024 = new Body0024();
        body0024.setName("test");
        System.out.println(XmlUtil.toXml(body0024));
        
        Body0020 body0020 = XmlUtil.toBean("<Body><Sn>10</Sn><BodyType>Body0024</BodyType></Body>", Body0020.class);
        System.out.println(String.format("%s[Sn:%s]", body0020.getBodyType(), body0020.getSn()));
	}
	
	public void testPerson() throws Exception{
		System.out.println("------xml序列化------");
		System.out.println(serializable());
		System.out.println("++++++xml序列化++++++\n");
		
		System.out.println("------xml反序列化------");
		System.out.println(deserializable());
		System.out.println("++++++xml反序列化++++++");
	}
	
	//序列化
	public String serializable(){
		Person person = new Person();
		person.setName("路人甲");
		
		Address address = new Address();
		address.setAddress("普天同庆路250号4栋514室");
		address.setAddress("中国梦路250号4栋514室");
		person.setAddress(address);
		
		Friend friend1 = new Friend();
		friend1.setId(1);
		friend1.setName("路人乙");
		person.setList(friend1);
		
		Friend friend2 = new Friend();
		friend2.setId(2);
		friend2.setName("路人丙");
		person.setList(friend2);
		
		String xml = XmlUtil.toXml(person);
		
		return xml;
	}
	
	//反序列化
	public Person deserializable() throws Exception{
		BufferedReader br = new BufferedReader(new FileReader(new File("./xml/Person.xml")));
		String xml = "", line;
		
		while((line = br.readLine()) != null){
			xml += line;
		}
		br.close();
		
		return XmlUtil.toBean(xml, Person.class);
	}
}
